/**
 * Created by 小虎Oni on 2016/9/3.
 */
'use strict';
app.controller('AddressCtrl', function($scope,UserService,TokenService,$state) {
  $scope.selectAddress = function(item){
    alert("选择了地址")
  };

  /*地址管理*/
  $scope.goAddress = function () {
    $state.go("address")
  };

  /*新增地址*/
  $scope.goAddAddress = function () {
    $state.go("addAddress")
  };
  $scope.addressData = {}
  function getAddress(){
    TokenService.getLocalUserId().then(function (user_id) {
      TokenService.getLocalToken().then(function (token) {
        UserService.getAddressInfo(user_id,token).then(function (res) {
          $scope.addressData = res;
        })
      })
    })
  }
  getAddress();
  $scope.deleteAddress = function (item) {
    UserService.delAddress(item.address_id).then(function (res) {
      if(res.response == 1){
        deleteLocalAddress(item);
      }
    })
  }
  function deleteLocalAddress(item){
    for(var key in $scope.addressData){
      if($scope.addressData[key].address_id == item.address_id){
        $scope.addressData.splice(key,1);
      }
    }
  }
});
